﻿#include "topwidget.h"
#include <QGridLayout>
#include <QStyleOption>
#include <QPainter>
#include <QDebug>

#if _MSC_VER >=1600
#pragma execution_character_set("utf-8")
#endif

QTopWidget::QTopWidget(QString sAppName,QString sIcon,QWidget *parent) : QWidget(parent)
{

    this->setObjectName("topwidget");
    this->setMaximumHeight(60);

    m_pParentWidget = parent;

    m_bMax = true;
    m_bleftBtnClk = false;

    this->setObjectName("topwidget");

    //软件名称显示
    QString sName = QString("<div><span style = 'font-weight:bold;font-family:Microsoft YaHei;font-size:28px; color:#ffffff;'>%2</span></div>").arg(sAppName);
    m_pTitleLabel = new QLabel(sName, this);
    m_pTitleLabel->setAlignment(Qt::AlignLeft);

    m_pIconLabel = new QLabel(this);
    if(sIcon != "")
    {
        m_pIconLabel->setMinimumWidth(300);
        QImage resultImg;
        resultImg.load(sIcon);
        resultImg = resultImg.scaled(m_pIconLabel->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
        m_pIconLabel->setPixmap(QPixmap::fromImage(resultImg));
    }
    else
    {
        m_pIconLabel->setHidden(true);
    }



    m_pTimeLabel = new QLabel("");
    m_pTimeLabel->setStyleSheet("font-size:15px; font-family: 'Microsoft YaHei';color:white");

    QWidget* pwidget1 = new QWidget();
    QGridLayout* playout1 = new QGridLayout(pwidget1);
    playout1->setContentsMargins(0, 0, 0, 0);
    playout1->setSpacing(0);

    playout1->setColumnStretch(0,4);
    playout1->setColumnStretch(1,1);
    playout1->setColumnStretch(3,40);

//    playout1->addWidget(m_pIconLabel,0,0,1,1,Qt::AlignLeft);
    playout1->addWidget(m_pTitleLabel,0,0,1,1,Qt::AlignLeft);






    //最大化  最小化 关闭按钮
    m_pMinButton = new QPushButton();
    m_pMaxButton = new QPushButton();
    m_pCloseButton = new QPushButton();

    m_pMinButton->setIcon(QIcon(":res/titlemini.png"));
    m_pMinButton->setMinimumHeight(30);
    m_pMinButton->setMaximumWidth(30);
    m_pMinButton->setToolTip("最小化");

    m_pMaxButton->setIcon(QIcon(":res/titlemax.png"));
    m_pMaxButton->setMinimumHeight(30);
    m_pMaxButton->setMaximumWidth(30);
    m_pMaxButton->setToolTip("最大化");

    m_pCloseButton->setIcon(QIcon(":res/titleclose.png"));
    m_pCloseButton->setMinimumHeight(30);
    m_pCloseButton->setMaximumWidth(30);
    m_pCloseButton->setToolTip("关闭");









    QGridLayout* playout = new QGridLayout(this);


    playout->addWidget(pwidget1,0,0,1,1);
    playout->addWidget(m_pTimeLabel,0,1,1,1);

    playout->addWidget(m_pMinButton,0,2,1,1);
    playout->addWidget(m_pMaxButton,0,3,1,1);
    playout->addWidget(m_pCloseButton,0,4,1,1);



    this->setMinimumHeight(50);


    //绑定事件
    connect(m_pMinButton,&QPushButton::clicked,this,&QTopWidget::onMinButtonclicked);
    connect(m_pCloseButton,&QPushButton::clicked,this,&QTopWidget::onCloseButtonclicked);
    connect(m_pMaxButton,&QPushButton::clicked,this,&QTopWidget::onMaxButtonclicked);


    //应用样式
    QFile file(":/res/topwidgetui.qss");
    file.open(QFile::ReadOnly);
    QString stylesheet = file.readAll();

    this->setStyleSheet(stylesheet);


    //定时器
    m_pTimer = new QTimer(this);
    connect(m_pTimer,&QTimer::timeout,this,[=]()
    {
        QDateTime l_time = QDateTime::currentDateTime();
//        l_time = l_time.addSecs(3600 * 8);

        QString str = l_time.toString("hh:mm:ss  ");
        m_pTimeLabel->setText(str);
    });


//    m_pTimer->start(1000);
    m_pTimeLabel->hide();

}


/******************************************
* 功能:最小化功能
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QTopWidget::onMinButtonclicked()
{
    m_pParentWidget->showMinimized();
}
/******************************************
* 功能:关闭功能
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QTopWidget::onCloseButtonclicked()
{
    if(m_pParentWidget != nullptr)
    {
        m_pParentWidget->close();
    }

}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QTopWidget::onMaxButtonclicked()
{
    return;
    if(m_bMax == true)
    {
        m_pParentWidget->setWindowState(Qt::WindowState::WindowNoState);
        m_pParentWidget->resize(1424,800);
        m_bMax = false;
    }
    else
    {
        m_pParentWidget->setWindowState(Qt::WindowState::WindowMaximized);
        m_bMax = true;
    }
}


void QTopWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}


void QTopWidget::mousePressEvent(QMouseEvent *event)
{
    if( event->button() == Qt::LeftButton)
    {
        m_startPos = event->globalPos();
        m_bleftBtnClk = true;
    }
    event->ignore();//表示继续向下传递事件，其他的控件还可以去获取
}

void QTopWidget::mouseReleaseEvent(QMouseEvent *event)
{

    if( event->button() == Qt::LeftButton ){
        m_bleftBtnClk = false;
    }
    event->ignore();
}

void QTopWidget::mouseMoveEvent(QMouseEvent *event)
{
    if( m_bleftBtnClk&& m_bMax == false)
    {
        QPoint l_current = event->globalPos();
        m_pParentWidget->move( m_pParentWidget->pos() + l_current - m_startPos );
        m_startPos = l_current;
    }
    event->ignore();
}

